package com.pengshun.modules.security;

import com.alibaba.fastjson.JSON;
import com.pengshun.modules.api.mapper.PsUserInfoMapper;
import com.pengshun.modules.api.coal.util.SystemPath;
import com.pengshun.modules.common.BaseResult;
import com.pengshun.modules.domain.User;
import com.pengshun.modules.jwt.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
public class AjaxAuthenticationSuccessHandler extends HandlerInterceptorAdapter implements AuthenticationSuccessHandler {

     private Long expiration=864000000L;

     @Autowired
     private PsUserInfoMapper psUserInfoMapper;

     private Long uiId = null;



     private String name = null;
     private String phone = null;

     @Autowired
     private RedisTemplate<String, String> redisTemplate;


     private final static String token = SystemPath.APP_TOKEN_PATH;

     @Override
     public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
          BaseResult result=new BaseResult(0000);
          SelfUserDetails userDetails = (SelfUserDetails) authentication.getPrincipal();
          JwtTokenUtil util = new JwtTokenUtil();

          User info = psUserInfoMapper.findUserByPhone(userDetails.getUsername());
          uiId = info.getId();
          name = info.getName();
          phone = info.getPhone();
          String jwtToken = "";
          jwtToken = redisTemplate.opsForValue().get(token + uiId);
          if (jwtToken == null || jwtToken == "") {
               jwtToken = util.generateToken(info);
               redisTemplate.opsForValue().set(token + uiId, jwtToken, expiration, TimeUnit.SECONDS);
          }
          result.setData(jwtToken);
          httpServletResponse.setCharacterEncoding("UTF-8");
          httpServletResponse.getWriter().write(JSON.toJSONString(result));
          String openid = httpServletRequest.getParameter("openid");
          if (openid != null && openid != "") {
//               CompanyEmployeeMp mp = new CompanyEmployeeMp(ceId, openid);
//               ResponseEntity<BaseResult> res = restTemplate.postForEntity(ConstantVo.GET_UPDATE_WECHAT_URL, mp, BaseResult.class);
          }

     }

     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
          HandlerMethod handlerMethod = (HandlerMethod) handler;
          request.setAttribute("uiId", uiId);
          request.setAttribute("name", name);
          request.setAttribute("phone", phone);
          return true;
     }
}
